/*
 ******************************
 * Object Oriented Programming in C
 *
 * Author: Laurent Deniau, Laurent.Deniau@cern.ch
 *
 * For more information, please see the paper:
 * http://home.cern.ch/ldeniau/html/oopc/oopc.html
 *
 ******************************
 */

/*------------------------------------*/
/*   Generic memBlock implementation  */
/*          [abstract class]          */
/*------------------------------------*/

#define IMPLEMENTATION

#include <g_memBlock.h>

/*
  Object implementation
*/

void
methodDecl_(set) size_t n, gType1 x __
{
  size_t i;

  for(i=0; i<n; i++) this->m.base[i] = x;
}

void
methodDecl_(copy) size_t n, gType1 const* t __
{
  size_t i;

  for(i=0; i<n; i++) this->m.base[i] = t[i];
}

void
methodDecl_(resize) size_t n __
{
  if (this == this->m.owner) { /* only owner can resize */
    this->m.base = realloc(this->m.base, n*sizeof(gType1));
  }
}

BASEOBJECT_IMPLEMENTATION

  methodName(set),
  methodName(copy),
  methodName(resize)

ENDOF_IMPLEMENTATION

/*
  Abstract Class implementation
*/

initClassDecl() {} /* required */

dtorDecl() /* required */
{
  if (this == this->m.owner) { /* only owner can destroy */
    free(this->m.base);
    this->m.owner = NULL;
    this->m.base  = NULL;
    --OBJECT.total;
  }
}

int
classMethodDecl(numBlock)
{
  return OBJECT.total;
}

void
methodDecl_(init) size_t n __
{
  this->m.owner = this;
  this->m.base  = calloc(n, sizeof(gType1));
  ++OBJECT.total;
}

void
methodDecl_(initRef) gType1 *const t __
{
  this->m.owner = NULL;
  this->m.base  = t;
}

gType1 const
constMethodDecl(*const_base)
{
  return this->m.base;
}

gType1
methodDecl(*base)
{
  return this->m.base;
}

ABSTRACTCLASS_IMPLEMENTATION

  /* init total */
  0,

  methodName(numBlock),
  methodName(init),
  methodName(initRef),
  methodName(const_base),
  methodName(base)

ENDOF_IMPLEMENTATION
